package com.fun.feifun.modules.business.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fun.feifun.base.utils.ControlUtils;
import com.fun.feifun.base.utils.RedisUtil;
import com.fun.feifun.modules.business.model.BusinessOrder;
import com.fun.feifun.modules.business.model.param.BusinessBlacklistPage;
import com.fun.feifun.modules.business.model.param.BusinessSummaryPage;
import com.fun.feifun.modules.sys.parameter.constant.ParameterConstant;
import com.fun.feifun.modules.sys.parameter.model.SysParameter;
import com.fun.feifun.modules.sys.parameter.service.ISysParameterService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fun.feifun.modules.business.service.IBusinessBlacklistService;
import com.fun.feifun.modules.business.model.BusinessBlacklist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fun.feifun.base.result.Result;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 业务黑名单手机号 前端控制器
 * </p>
 *
 * @author wkf
 * @since 2024-09-22
 */
@RestController
@Api(value = "/business/business-blacklist", tags = "业务黑名单手机号")
@Slf4j
@RequestMapping("/business/business-blacklist")
public class BusinessBlacklistController {


    @Autowired
    private IBusinessBlacklistService service;
    @Autowired
    private ISysParameterService parameterService;

    /**
     * 获取分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "获取分页信息")
    public Result<Page<BusinessBlacklist>> page(@RequestBody BusinessBlacklistPage param){
        QueryWrapper<BusinessBlacklist> wrapper=new QueryWrapper<>();
        if(ControlUtils.isNotEmpty(param.getLockStatus())){
            wrapper.lambda().eq(BusinessBlacklist::getLockStatus,param.getLockStatus());
        }
        if(ControlUtils.isNotEmpty(param.getPhone())){
            wrapper.lambda().like(BusinessBlacklist::getPhone,param.getPhone());
        }
        Page page = new Page(param.getPage(), param.getSize());
        service.page(page);
        return Result.success(page);
    }

    /**
     * 添加数据
     */
    @PostMapping(value = "/save")
    @ApiOperation(value = "添加或者保存")
    public Result save(@RequestBody BusinessBlacklist item){
        if(service.saveOrUpdate(item)){
         return Result.success("数据添加成功！");
        }
        return Result.err("数据添加成功！");
    }

    /**
     * 删除数据
     */
    @GetMapping("/delete")
    @ApiOperation(value = "")
    public Result delete(@RequestParam("phone")String phone ){
        if(service.delete(phone)){
            return Result.success("数据删除成功！");
        }
        return Result.err("数据删除失败");
    }

    /**
     * 清空黑名单
     */
    @GetMapping("/clear")
    @ApiOperation(value = "情况黑名单")
    public Result clear(){
        if(service.clear()){
            return Result.success();
        }
        return Result.err("");
    }


    @GetMapping("/getThresholdCount")
    @ApiOperation(value = "获取阈值")
    public Result getThresholdCount(){
        Integer thresholdCount= ParameterConstant.BLACKLIST_THRESHOLD_DEFAULT_VALUE;
        String threshold = parameterService.getParameByCode(ParameterConstant.BLACKLIST_THRESHOLD);
        if(StringUtils.isNotEmpty(threshold)){
            thresholdCount=Integer.parseInt(threshold);
        }
        return Result.success(thresholdCount);
    }

    @GetMapping("/getAutoStatus")
    @ApiOperation(value = "获取自动状态")
    public Result getAutoStatus(){
        Boolean status= ParameterConstant.BLACKLIST_AUTH_CLREAR_DEFAULT_VALUE;
        String threshold = parameterService.getParameByCode(ParameterConstant.BLACKLIST_AUTH_CLREAR);
        if(StringUtils.isNotEmpty(threshold)){
            status=Boolean.parseBoolean(threshold);
        }
        return Result.success(status);
    }

    @GetMapping("/setThresholdCount")
    @ApiOperation(value = "设置阈值")
    public Result setThresholdCount(@RequestParam("value") String value){
        UpdateWrapper<SysParameter> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(SysParameter::getCode,ParameterConstant.BLACKLIST_THRESHOLD).set(SysParameter::getValue,value);
        boolean update = parameterService.update(wrapper);
        if(update){
            return Result.success();
        }
        return Result.err("");
    }

    @GetMapping("/setAutoStatus")
    @ApiOperation(value = "设置自动清理黑名单")
    public Result setAutoStatus(@RequestParam("value") String value){
        UpdateWrapper<SysParameter> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(SysParameter::getCode,ParameterConstant.BLACKLIST_AUTH_CLREAR).set(SysParameter::getValue,value);
        boolean update = parameterService.update(wrapper);
        if(update){
            return Result.success();
        }
        return Result.err("");
    }
}

